import java.util.ArrayList;
import java.util.List;


public class AnimObj {
	
	static double WALK_SPEED = 100;
	static double A1_SPEED = 100;
	static double A2_SPEED = 100;

	float x;
	float y;
	
	
	List<String> sprite;
	int cur_sprite;
	
	int 	animation;
	//	0 - STAND
	//	1 - WALK
	//	2 - KICK1
	//	3 - KICK2
	int playable_anim;
	// -1 - No animation to play once
	
	int		anim_frame;
	double	anim_timer;
	int		walk_frames;
	int		a1_frames;
	int		a2_frames;
	
	AnimObj( String name, int walkFrames, int a1Frames, int a2Frames ) {
		x = 0;
		y = 0;
		animation = 0;
		playable_anim = -1;
		anim_timer = 0;
		cur_sprite = 0;
		walk_frames = walkFrames;
		a1_frames = a1Frames;
		a2_frames = a2Frames;
		sprite = new ArrayList<String>();
		
		sprite.add(name + "0.png");
		for (int i = 0; i < walkFrames; i++) {
			String tmp = new String();
			tmp = String.valueOf(i+1);
			sprite.add(name + tmp + ".png");
		}	
		for (int i = 0; i < a1Frames; i++) {
			String tmp = new String();
			tmp = String.valueOf(i+1+walkFrames);
			sprite.add(name + tmp + ".png");
		}	
		for (int i = 0; i < a2Frames; i++) {
			String tmp = new String();
			tmp = String.valueOf(i+1+walkFrames+a1Frames);
			sprite.add(name + tmp + ".png");
		}	
			
	}
	
	public void playAnimation(int anim) {
		playable_anim = anim;
		anim_frame = 0;
		anim_timer = 0;
	}
	public void setAnimation(int anim) {
		if (animation != anim) {
			animation = anim;
			if (playable_anim == -1) {
				anim_frame = 0;
				anim_timer = 0;
			}
		}
	}
	public void animate(long dt) {
		int save_anim = animation;
		
		if (playable_anim != -1) {	
			animation = playable_anim;
		}
		
		if (animation == 0) { 		//STAND
			cur_sprite = 0;
			anim_timer = 0;
		}
		else if (animation == 1) {	//WALK	
			anim_timer += dt;
			if (anim_timer > WALK_SPEED) {
				anim_frame++;
				anim_timer = 0;
				if (anim_frame >= walk_frames) {
					anim_frame = 0;
					playable_anim = -1;
				}
			}
			cur_sprite = 1 + anim_frame;
		}
		else if (animation == 2) {	//A1
			anim_timer += dt;
			if (anim_timer > A1_SPEED) {
				anim_frame++;
				anim_timer = 0;
				if (anim_frame >= a1_frames) {
					anim_frame = 0;
					playable_anim = -1;
				}
			}
			cur_sprite = 1 + walk_frames + anim_frame;
		}
		else if (animation == 3) {	//A2
			anim_timer += dt;
			if (anim_timer > A2_SPEED) {
				anim_frame++;
				anim_timer = 0;
				if (anim_frame >= a2_frames) {
					anim_frame = 0;
					playable_anim = -1;
				}
			}
			cur_sprite = 1 + walk_frames + a1_frames + anim_frame;
			animation = save_anim;
		}
	}
}
